﻿// Autore: Emanuele Poggi

using System.Linq;
using NUnit.Framework;
using RecipesManager.Data;
using TechTalk.SpecFlow;
using White.Core.Factory;
using White.Core.UIItems;
using White.Core.UIItems.WindowItems;

namespace RecipesManager.Specs.Evento
{
    [Binding]
    [Scope(Feature = "RimuoviEvento")]
    class RimuoviEvento_Steps:EventiBdd
    {
        Window _dialog;
        Button _confirm;
        Button _cancel;

        void GetConfirmDialogUi()
        {
            _dialog = MainWindow.ModalWindows()[0];
            _confirm = MainWindow.Get<Button>("6");
            _cancel = MainWindow.Get<Button>("7");
        }

        [Then(Regex = @"appare un messaggio")]
        public void AppareMessaggio()
        {
            Assert.That(MainWindow.ModalWindows().Count() == 1);
        }

        [When(Regex = "confermo")]
        public void QuandoConfermo()
        {
            this.GetConfirmDialogUi();
            _confirm.Click();
            Assert.That(!MainWindow.ModalWindows().Any());
        }

        [When(Regex = "rifiuto")]
        public void QuandoRifiuto()
        {
            this.GetConfirmDialogUi();
            _cancel.Click();
            Assert.That(!MainWindow.ModalWindows().Any());
        }

        [BeforeScenario]
        public new void SetUpScenario()
        {
            base.SetUpScenario();
        }

        [AfterScenario]
        public new void TearDownScenario()
        {
            base.TearDownScenario();
        }

        [AfterStep]
        [BeforeStep]
        public void MiniStop()
        {
            MainWindow.WaitWhileBusy();
            MainWindow.ReloadIfCached();
            MainWindow.ReInitialize(InitializeOption.NoCache);
        }

        [Given(Regex = "esiste l'evento \"([a-zA-Z ]+)\" \"([a-zA-Z ]+)\"")]
        public void DatoEsisteEvento(string stato , string evento)
        {
            var e = Manager.Events.Add(evento);
            if (stato == "chiuso")
                e.Close();
        }

        [When(Regex = "elimino l'evento \"([a-zA-Z ]+)\" \"([a-zA-Z ]+)\"")]
        public void QuandoEliminoEvento(string stato, string evento)
        {
            NavigateToEvents();
            GetEventUi();

            if (stato == "chiuso")
            {
                ClosedEvents.Click();
                GetPassedEventsUi();
                ClosedEventsList.Rows[0].Click();             
            }else
            {
                EventsList.Rows[0].Click();
            }
            RemoveEvent.Click();
        }

        [Then(Regex = "non esiste l'evento \"([a-zA-Z ]+)\"")]
        public void AlloraoNonEsisteEvento(string evento)
        {
            var events = Manager.Events.AsQueryable();
            var ev = events.Where(e => e.Name == evento);

            Assert.That(!ev.Any());
        }
    }
}
